package calculate24.play.IndexActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import calculate24.play.AutoCalculate.AutoCalculate;
import calculate24.play.R;
import calculate24.play.SelfCalculate.SelfCalculate;

//主界面
public class IndexActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);

        //“闭关修炼”按钮
        Button startSelfCalculate = findViewById(R.id.startSelfCalculate);
        ActivityResultLauncher<Integer> launcher = registerForActivityResult(new ActivityResultContract<Integer, Boolean>() {
            @NonNull
            @Override
            public Intent createIntent(@NonNull Context context, Integer input) {
                return new Intent(IndexActivity.this, SelfCalculate.class);
            }

            @Override
            public Boolean parseResult(int resultCode, @Nullable Intent intent) {
                if(intent == null) {
                    return false;
                }
                return intent.getBooleanExtra("replay?", false);
            }
        }, new ActivityResultCallback<Boolean>() {
            @Override
            public void onActivityResult(Boolean result) {
                if(result) {
                    startSelfCalculate.callOnClick();
                }
            }
        });
        startSelfCalculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launcher.launch(null);
            }
        });

        //“武功秘籍”按钮
        Button startButton = findViewById(R.id.startButton);
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(IndexActivity.this, AutoCalculate.class);
                startActivity(intent);
            }
        });

    }
}